#include "fifo.hpp"

// #define FIFO_FILE "fifo"

// #define EXIT_FAILURE 1

// #define _EXIT(m) \ 
// do \
// { \
//     perror(m);\
//     exit(EXIT_FAILURE)); \
// }while(0)

// #define ERR_EXIT(m) \
// do \
// { \
//     perror(m); \
//     exit(EXIT_FAILURE); \
// } while(0)


int main()
{
    int ret = mkfifo(FIFO_FILE,0666);

    if(ret < 0)
    {
        ERR_EXIT("MKFIFO ERROR!");
    }

    int fd = open(FIFO_FILE,O_RDONLY | O_CREAT,0666);

    if(fd < 0)
    {
        ERR_EXIT("OPEN_ERROR");
    }

    char buffer[1024] = { 0 };

    // std::string s;

    // 服务端应该是是负责写入的。
    while(true)
    {
        int n = read(fd,buffer,sizeof(buffer));

        if(n > 0)
        {
            buffer[n] = 0;
        }
        else 
        {
            ERR_EXIT("READ ERROR!");
        }

        buffer[n] = 0;

        std::cout << "Client say #  " << buffer << std::endl;
    }

    close(fd);

    return 0;
}